package com.gsn.poker.play;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Align;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
import com.badlogic.gdx.scenes.scene2d.ui.FlickScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.GsnImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.gsn.dataCommon.UserData;
import com.gsn.desktop.engine.ActorUtility;
import com.gsn.engine.gdx.GsnParticleActor;
import com.gsn.engine.myplay.GsnLayer;
import com.gsn.engine.utility.UtilityGettor;
import com.gsn.name.NameEdit;
import com.gsn.poker.asset.ChatTexture;
import com.gsn.poker.asset.FontTexture;
import com.gsn.poker.asset.PokerSound;
import com.gsn.poker.asset.PokerTexture;
import com.gsn.poker.asset.PortalTexture;
import com.gsn.poker.game.MyPoker;
import com.gsn.poker.packet.PacketFactory;

public class ChatLayer extends GsnLayer implements ClickListener {
	private static final String tag = "DialogLayer";
	float time = 0.5f;
	protected ParticleEffect clickEff;
	public int id;
	public TextField labelChatField;
	GsnImageButton close;
	GsnImageButton send;

	public ChatLayer(float width, float height) {
		super(width, height);

		ChatTexture chatTexture = ChatTexture.inst();

		Image bg = new Image(PokerTexture.inst().dialogBg);

		addActor(bg);
		bg.width = width;
		bg.height = height;

		Label labelTitle = new Label("Chat", new Label.LabelStyle(FontTexture.inst().fontMedium, new Color(1, 1, 1, 1)));
		addActor(labelTitle);
		labelTitle.x = bg.x + bg.width * 0.05f;
		labelTitle.y = bg.y + bg.height * 0.92f;

		Image textImage = new Image(PortalTexture.inst().textBoxWhite);
		PortalTexture.inst().scaleActor(textImage);
		addActor(textImage);
		textImage.width = 0.7f * width;
		textImage.height = height * 0.09f;
		textImage.x = 0.1f * width;
		textImage.y = 0.75f * height;

		labelChatField = new TextField(PortalTexture.inst().skin);
		addActor(labelChatField);
		labelChatField.x = textImage.x;
		labelChatField.y = textImage.y;

		// if (UtilityGettor.onMobile) {
		// UtilityGettor.inst().load();
		labelChatField.visible = false;

		UtilityGettor.inst().makeTextFieldWithScreenCoordinate(this, PortalTexture.inst().skin.getStyle(TextField.TextFieldStyle.class), NameEdit.CHAT, "", textImage.width, textImage.height, height - textImage.y - textImage.height, textImage.x);
		UtilityGettor.inst().hideTextField(NameEdit.CHAT);
		// }

		close = new GsnImageButton(chatTexture.closeBtn, chatTexture.closeBtnDown);
		chatTexture.scaleActor(close);
		addActor(close);
		close.x = width * 0.96f - close.getRealWidth();
		close.y = height - close.getRealHeight()* 1.05f;
		close.setClickListener(this);

		send = new GsnImageButton(chatTexture.sendBtn, chatTexture.sendBtnDown);
		chatTexture.scaleActor(send);
		addActor(send);
		send.x = width * 0.95f - send.getRealWidth();
		send.y = height * 0.73f;
		send.setClickListener(this);

		ChatGroup chatGroup = new ChatGroup(width);

		FlickScrollPane scrollChat = new FlickScrollPane(chatGroup);
		addActor(scrollChat);

		scrollChat.width = 0.9f * width;
		scrollChat.height = chatGroup.height;
		scrollChat.x = 0.05f * width;
		scrollChat.y = (0.75f * height - scrollChat.height) / 2;

		clickEff = PortalTexture.inst().clickEff;
	}

	@Override
	public void click(Actor actor, float x, float y) {
		// TODO Auto-generated method stub
		hide();
		if (clickEff != null) {
			Vector2 v = ActorUtility.getCoordinate(actor, x, y, this.root);
			GsnParticleActor par = new GsnParticleActor(clickEff);
			par.setPostition(v.x, v.y);
			par.start();
			this.addActor(par);
		}
		if (actor == send) {
			String s = UtilityGettor.inst().getString(NameEdit.CHAT);
			if (!s.equals(""))
				MyPoker.client.send(PacketFactory.createChat(s));
		}
	}

	public InputProcessor oldInput;

	public void show() {
		oldInput = Gdx.input.getInputProcessor();
		UtilityGettor.inst().visibleTextField(NameEdit.CHAT);
		visible = true;
		setInputListener();
	}

	public void hide() {
		Gdx.input.setInputProcessor(oldInput);
		UtilityGettor.inst().hideTextField(NameEdit.CHAT);
		oldInput = null;
		visible = false;
	}

	@Override
	public boolean keyDown(int keycode) {
		switch (keycode) {
		case Keys.A:
			for (GsnLayer l : MyPoker.game.chooseChannelScreen.layers) {
				System.out.println(l.getClass().getName());
			}
			break;
		}
		return super.keyDown(keycode);
	}

}
